#include "ubvm_line.h"
#include "DIVA_Manager.h"
#include <sstream>
#include "xParameterPack.h"
#include "vmLibrary.h"
// 
//  name: ypc
//  history:  2009/3/10 
//            The original file x_cuboid_int_ptvm is copied to be the tempory body.

//Name           : hyt
//Update Date    : 08/10/23
//Update History :
//				(1) VL_createEntity 必須給 meshname,material可給可不給,若想自行命名vm的開頭,則可以
//                   再加第三個參數 string specialhead
//              (2) VL_regBoudingBox 則需要 vm的entity 
//              (3) 由programmer自行決定node內容
//              (4) 若要在vm任何一個位置顯示文字,首先呼叫VL_createVMText(你想顯示的文字,文字node的開頭name)
//                  再來是呼叫VL_createTextNode(vm文字位置)
//              (5) 設定vm node顏色則呼叫VL_setEntityColor(vm entity , Vector4(colorvalue))來設定

ubvm_line::ubvm_line(xParameterPack xpp):DIVA_VM(xpp){
	int defaultport = vmLibrary::instance().get_vm_default_input_parameters().size();
	_sx = xpp.paras[defaultport].val_float_ptr;
	_sy = xpp.paras[defaultport+1].val_float_ptr;
	_sz = xpp.paras[defaultport+2].val_float_ptr;
	_ex = xpp.paras[defaultport+3].val_float_ptr;
	_ey = xpp.paras[defaultport+4].val_float_ptr;
	_ez = xpp.paras[defaultport+5].val_float_ptr;
	//_startVM = xpp.paras[defaultport].val_vm;
	//_endVM = xpp.paras[defaultport+1].val_vm;
	
	init_default_parameters();

	VL_addVMAnimation();
	enableVMAnimation();
}

/// destructor
ubvm_line::~ubvm_line(){
}

void ubvm_line::load_3Dresources() {	
	_myLine = new Line3D();
	//_myLine->drawLine(Vector3(0,0,0), Vector3(200,200,200));
	_line_node = DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->createChildSceneNode();
	_line_node->attachObject(_myLine);
	_myLine->drawLine(Ogre::Vector3(*_sx,*_sy,*_sz),Ogre::Vector3(*_ex,*_ey,*_ez));
}
void ubvm_line::set_default_values() {
	_vm_rx_val = 0 ;
	_vm_ry_val = 0 ;
	_vm_rz_val = 0 ;

	_vm_init_sizex = 1 ;
	_vm_init_sizey = 1 ;
	_vm_init_sizez = 1 ;

	_vm_sizex_val = 1 ;
	_vm_sizey_val = 1 ;
	_vm_sizez_val = 1 ;

	_vm_sizexscale_val = 1.0 ;
	_vm_sizeyscale_val = 1.0 ;
	_vm_sizezscale_val = 1.0 ;

	_vm_r_val = 0.0;
	_vm_g_val = 0.0;
	_vm_b_val = 0.0;
	_vm_transparency_val = 1.0 ;

	_vm_yaw_val = 0 ;
	_vm_pitch_val = 0 ;
	_vm_roll_val = 0 ;

}

void ubvm_line::reg_event_entity() {
}
void ubvm_line::VMValueUpdated(){
	DIVA_VM::VMValueUpdated();
	if (_line_node != NULL) {
		DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->removeChild(_line_node);
		delete _line_node ;
		delete _myLine ;
	}
	_myLine = new Line3D();
	setColorTo3DLine();
	_line_node = DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->createChildSceneNode();
	_line_node->attachObject(_myLine);
	_myLine->drawLine(Ogre::Vector3(*_sx,*_sy,*_sz),Ogre::Vector3(*_ex,*_ey,*_ez));
}
void ubvm_line::setBodyScale(Ogre::Vector3 scale) {
	
}
bool ubvm_line::isBodyScaleWrtSceneNode() { return true ; }

void ubvm_line::VMPicked(Ogre::Real x, Ogre::Real y, Ogre::Real z){

}
void ubvm_line::VMAnimation(const Ogre::FrameEvent &evt){
	//start =_startVM->getPosition();
	//end   =_endVM->getPosition();
	if (_line_node != NULL) {
			DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->removeChild(_line_node);
			delete _line_node ;
			delete _myLine ;
	}
	_myLine = new Line3D();
	setColorTo3DLine();
	//_myLine->drawLine(Vector3(0,0,0), Vector3(200,200,200));

	_line_node = DIVA_Manager::getInstance()->getSceneManager()->getRootSceneNode()->createChildSceneNode();
	_line_node->attachObject(_myLine);
	_myLine->drawLine(Ogre::Vector3(*_sx,*_sy,*_sz),Ogre::Vector3(*_ex,*_ey,*_ez));

	// update line name position
	if (_name_scene_node)
	{
		Ogre::Real x, y, z;
		x = computeCenter(*_sx, *_ex);
		y = computeCenter(*_sy, *_ey);
		z = computeCenter(*_sz, *_ez);
		_name_scene_node->setPosition(Ogre::Vector3(x,y,z));
	}
}

void ubvm_line::init_color_and_transparency()
{
	assert(_vm_r_val >= 0.0 && _vm_g_val >= 0.0 && _vm_b_val >= 0.0) ;
	// the three variables should be set correctly in set_default_values

	if(_vm_r) _vm_r_val = *_vm_r ;
	if(_vm_g) _vm_g_val = *_vm_g ;
	if(_vm_b) _vm_b_val = *_vm_b ;

	if(_vm_transparency)  _vm_transparency_val = (*_vm_transparency);

	assert((_vm_r_val) >= 0.0 && (_vm_r_val) <= 1.0);
	assert((_vm_g_val) >= 0.0 && (_vm_g_val) <= 1.0);
	assert((_vm_b_val) >= 0.0 && (_vm_b_val) <= 1.0);
	assert((_vm_transparency_val) >= 0.0 && (_vm_transparency_val) <= 1.0);

}

void ubvm_line::setColorTo3DLine()
{
	_myLine->init_color_and_transparency(Ogre::Vector4(_vm_r_val, _vm_g_val, _vm_b_val, _vm_transparency_val));
}

Ogre::Real ubvm_line::computeCenter( float start, float end )
{
	Ogre::Real center;
	if ( start < end ){
		center = ( end - start )/2 + start;
	} else {
		center = ( start - end )/2 + end;
	}
	return center;
}
